package com.baosight.sczx.zp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.platform.core.security.base.Filter;
import com.baosight.platform.core.security.base.SecurityFactory;
import com.baosight.platform.core.security.base.SortCondition;
import com.baosight.platform.core.security.resource.Resource;
import com.baosight.platform.core.security.resourceset.IResourcesetManager;
import com.baosight.sczx.zp.domain.ZPResource;

/**
 * <p>Title: ServiceZP23.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 梁琦
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-18 上午06:27:44  梁琦  Create
 *</pre>
 */
public class ServiceZP23 extends ServiceEPBase {
	
	@SuppressWarnings("unused")
	private IResourcesetManager rManager = SecurityFactory.getInstance().getResourcesetManager();

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询资源
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EiInfo query(EiInfo inInfo) {	//queryRes
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock block = outInfo.addBlock(EiConstant.resultBlock);
		block.setBlockMeta(new ZPResource().eiMetadata);
		
		String resgLabel = inInfo.getString("Name");

		QueryMap queryMap = QueryMap.getQueryMap(inInfo);
		
		Filter filter = new Filter();

		SortCondition sort = new SortCondition();
		sort.addAsc("Name");

		List ress = rManager.getResourcesOfResourceset(resgLabel, filter, queryMap.getOffset(), queryMap.getLimit(), sort);

		for (int i = 0; i < ress.size(); i++) {				
			Resource res = (Resource) ress.get(i);
			ZPResource row = new ZPResource();
			row.setFormEname(res.getName());
			row.setFormCname(getFormCname(res.getName()));
			row.setFormType(res.getResourceType());
			block.addRow(row);
		}		
		int num = rManager.getResourceCountOfResourceset(resgLabel, filter);
		queryMap.setCount(num);
		queryMap.setEiInfo(outInfo, EiConstant.resultBlock);
		return outInfo;
		

	}
	
	/**
	 * 页面中文名
	 * <p>Function: getFormCname</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	public String getFormCname( String formEname )
	{
		
		Map param = new HashMap();
		param.put( "form_ename", formEname );
		
		String formCname = "";
		
		List forms = dao.query("ZPResource.query", param );
		if( forms.size() > 0 )
		{
			ZPResource form =  ( ZPResource )forms.get( 0 );
			formCname = form.getFormCname();
		}
		
		return formCname;
	}

	/**
	 * 增添时查询资源
	 * <p>Function: queryRes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	public EiInfo queryRes(EiInfo inInfo) {	
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock resultBlock = outInfo.addBlock("ResResult");
		resultBlock.setBlockMeta(new ZPResource().eiMetadata);
		
		QueryMap queryMap = QueryMap.getQueryMap(inInfo);
		Map map = new HashMap();
		map.put("Projectprefix", FrameworkInfo.getProjectPrefix());
		map.put("formEname", queryMap.get("formEname"));
		map.put("formCname", queryMap.get("formCname"));
		
		List<ZPResource> list = dao.query("ZPResource.query", map);
		
		String resgLabel = inInfo.getString("Name");
		if(resgLabel != null){
			List ress = rManager.getResourcesOfResourceset(resgLabel, new Filter(), 0, -1, new SortCondition());			
			for(ZPResource res : list){
				Boolean flag = true;
				for (int i = 0; i < ress.size(); i++) {				
					Resource resource = (Resource) ress.get(i);
					if(res.getFormEname().equals(resource.getName())){
						flag = false;
						break;
					}
				}
				if(flag)
					resultBlock.addRow(res);
			}
		}
		else
		{
			resultBlock.setRows(list);
		}
		return outInfo;
	}
	
	/**
	 * 删除资源
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EiInfo delete(EiInfo inInfo) {	//deleteRes
		String resgLabel = inInfo.getString("Name");

		

		List ress = inInfo.getBlock(EiConstant.resultBlock).getRows();
		for (int i = 0; i < ress.size(); i++) {
			Map res = (Map) ress.get(i);
			String resName = (String) res.get("formEname");
			String resTyp = (String) res.get("formType");
			rManager.removeMember(resgLabel, resName, resTyp);
		}
			

		return query(inInfo);	//queryRes

	}
	
	/**
	 * 增添资源
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午07:59:00 梁琦
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo insert(EiInfo inInfo) {	//insertResg
		String regName = inInfo.getString("Name");
		
		if(inInfo.getBlock("ResResult")!= null){
			for (int i = 0; i < inInfo.getBlock("ResResult").getRowCount(); i++) {
				Map res = (Map) inInfo.getBlock("ResResult").getRow(i);

				String resName = (String) res.get("formEname");
				rManager.addMember(regName, resName, res.get("formType").toString());				
			}
		}

		return inInfo;
	}

}
